package net.starwifi.duobao.web;

import net.starwifi.duobao.AppInitListener;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.entity.PaymentMethod;
import net.starwifi.duobao.mvc.*;
import net.starwifi.duobao.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;


/**
 * Created by Administrator on 2015/9/28.
 */
@Controller("apiOrderController")
@RequestMapping("/order")
public class OrderController extends BaseController {


    @Autowired
    OrderService orderService;

    @Autowired
    MemberService memberService;

    @Autowired
    PluginService pluginService;

    @Autowired
    PaymentMethodService paymentMethodService;

    /**
     * 账户充值(订单创建)
     * @param fee 充值金额
     */
    @ResponseBody
    @RequestMapping(value = "/recharge")
    public Object list(BigDecimal fee) {
        final String sn= AppInitListener.idGenerate.getID().toString();
        Member member=memberService.getCurrent();
        PaymentMethod paymentMethod=paymentMethodService.findByName("wo3DirectPlugin");
        orderService.build(paymentMethod, fee, sn, 1, member);
        return Message.createMessage(Message.success("success"), new HashMap<String, Object>() {{
            put("sn",sn);
        }});
    }



    /**
     * 账户支付
     * @param sn 订单编号
     */
    @RequestMapping(value = "/payment", method = RequestMethod.GET)
    public String payment(String sn,Model model) {
        model.addAttribute("order",orderService.findBySn(sn));
        return "/order/payment";
    }

}
